package edu.hawaii.myisern.action;

import java.io.IOException;
import javax.xml.bind.JAXBException;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.researchers.jaxb.Researcher;


/**
 * Handles the EditResearcher Action Bean.
 * 
 * @author Phillip Le
 * @author Sonwright M. Gomez
 *
 */
public class EditResearcherActionBean extends MyIsernActionBean {
  /** The singleton MyIsernModel. */
  private MyIsernModel myIsern = MyIsernModel.getInstance();
  private Researcher researcher;

  /** Private fields needed for researcher fields */
  @Validate(required = true)
  private String researcherName;
  private String researcherOrganization;
  private String researcherEmail;
  private String researcherPictureLink;
  private String researcherBioStatement;
  private String possibleResearcherName;
  private String oldResearcherName;
  private String errorMessage;
  private String mainMenu = "/mainMenu.jsp";
  
  /**
   * Get's the possible name.
   * 
   * @return possibleName The possible name.
   */
  public String getPossibleResearcherName() {
    return possibleResearcherName;
  }

  /**
   * Set's possible name.
   * 
   * @param name containing name.
   */
  public void setPossibleResearcherName(String name) {
    this.possibleResearcherName = name;
  }
  
  
  /**
   * Set's the error message for the user.
   * 
   * @param errorMessage containing error Message. 
   */
  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }
  
  /**
   * Returns the error message, which may be empty.
   * 
   * @return The error message. 
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }
  
  
  /**
   * Set's the researcher's name.
   * 
   * @param rName the name of the Researcher.
   */
  public void setResearcherName(String rName) {
    this.researcherName = rName;
  }

  /**
   * Get's the researcher's name.
   * 
   * @return ResearcherName The researcher's name.
   */
  public String getResearcherName() {
    return researcherName;
  }

  /**
   * Sets the researcher's organization.
   * 
   * @param rOrganization containing researcher's organization
   */
  public void setResearcherOrganization(String rOrganization) {
    this.researcherOrganization = rOrganization;
  }

  /**
   * Gets the researcher's organization.
   * 
   * @return the researcher's organization
   */
  public String getResearcherOrganization() {
    return researcherOrganization;
  }

  /**
   * Set's the researcher's email.
   * 
   * @param rEmail containing researcher's email address.
   */
  public void setResearcherEmail(String rEmail) {
    this.researcherEmail = rEmail;
  }

  /**
   * Get's the researcher's email address
   * 
   * @return the researcher's email address
   */
  public String getResearcherEmail() {
    return researcherEmail;
  }

  /**
   * Set's the researcher's Picture link.
   * 
   * @param rPictureLink containing the Url to the picture link.
   * 
   */
  public void setResearcherPictureLink(String rPictureLink) {
    this.researcherPictureLink = rPictureLink;
  }

  /**
   * Get's the Researcher's picture link.
   * 
   * @return the researcher's picture link.
   */
  public String getResearcherPictureLink() {
    return researcherPictureLink;
  }

  /**
   * Set's the Researcher's Bio Statement.
   * 
   * @param rBio containing the researcher's bio statement.
   */
  public void setResearcherBioStatement(String rBio) {
    this.researcherBioStatement = rBio;
  }

  /**
   * Get's the researcher's Bio Statement
   * 
   * @return the researcher's Bio Statement.
   */
  public String getResearcherBioStatement() {
    return researcherBioStatement;
  }
  
  

  /**
   * Populates the data for the research before editing.
   * 
   * @return A resolution to the editResearcher page.
   */
  @DontValidate
  public Resolution preEditResearcher() {
    try {
      if (myIsern.getResearcherIds().contains(getPossibleResearcherName().replace(' ', '_'))) {
        this.researcher = this.myIsern.getResearcher(this.possibleResearcherName);
        this.oldResearcherName = getResearcherName();
        setResearcherName(this.researcher.getName().trim());
        setResearcherOrganization(this.researcher.getOrganization());
        setResearcherEmail(this.researcher.getEmail());
        setResearcherPictureLink(this.researcher.getPictureLink());
        setResearcherBioStatement(this.researcher.getBioStatement().trim());
        return new RedirectResolution("/editResearcher.jsp").flash(this);
      }
      else {
        setErrorMessage("There was no match found in the Researcher " +
            "database. Please check spelling for any errors.");
        return new ForwardResolution (this.mainMenu);
      }
    }
    catch (NullPointerException e) {
      setErrorMessage("Researcher Field Blank.");
      return new ForwardResolution(this.mainMenu);
    }
  }
  
  /**
   * Add's a new researcher based on what user has entered in fields
   * 
   * @return Resolution that displays mainMenu.jsp
   * @throws IOException if IO error occurs.
   * @throws JAXBException if JAXB error occurs.
   */
  @DefaultHandler
  public Resolution submitAndSave() throws JAXBException, IOException {
    this.oldResearcherName = this.researcherName;
    if (getResearcherName().isEmpty()) {
      ValidationError error = new SimpleError("Requires a Researcher Name");
      getContext().getValidationErrors().add("researcherName", error);
      return getContext().getSourcePageResolution();
    }
    else {
      Researcher newR = new Researcher();

      // Add name
      newR.setName(this.researcherName.trim());

      // Add Researcher's organization
      try {
        newR.setOrganization(this.researcherOrganization.trim());
      }
      catch (NullPointerException e) {
        newR.setOrganization("");
      }

      // Add Researcher's email
      try {
        newR.setEmail(this.researcherEmail.trim());
      }
      catch (NullPointerException e) {
        newR.setEmail("");
      }
      // Add Researcher's Picture link
      try {
        newR.setPictureLink(this.researcherPictureLink.trim());
      }
      catch (NullPointerException e) {
        newR.setPictureLink("");
      }
      // Add Researcher's Bio Statement
      try {
        newR.setBioStatement(this.researcherBioStatement.trim());
      }
      catch (NullPointerException e) {
        newR.setBioStatement("");
      }
      
      myIsern.deleteResearcher(this.oldResearcherName);
      myIsern.addResearcher(newR);
      myIsern.saveResearcher();
      return new ForwardResolution(this.mainMenu);
    }
  }

  /**
   * Returns the user to the main menu without saving
   * 
   * @return Resolution that displays mainMenu.jsp
   */
  @DontValidate
  public Resolution cancel() {
    return new ForwardResolution(this.mainMenu);
  }
}
